﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Location
{
    public class Location:ILocation
    {
        public int AddLocation(decimal latitude, decimal longitude, string cityName, ref NutriLeaf.Web.Model.NutriLeafDBEntities context)
        {
            Model.Location loc = new Model.Location();
          
            loc.City.CityName = cityName;
            loc.Latitude = latitude;
            loc.Longitude = longitude;
            context.Locations.AddObject(loc);
            context.SaveChanges();
            return 0;
     
        }







        public int AddLocation(decimal latitude, decimal longitude, int countryId, ref Model.NutriLeafDBEntities context)
        {
            //doubt
            Model.Location loc = new Model.Location();
            Model.Country cont= context.Countries.Where(p => p.CountryID==countryId).First();
            loc.Longitude = longitude;
            loc.Latitude = latitude;
          
            context.Locations.AddObject(loc);
            context.SaveChanges();
            return 0;
     

        }

        public int AddLocation(decimal latitide, decimal longitude, ref Model.NutriLeafDBEntities context)
        {
            Model.Location loc = new Model.Location();
            loc.Longitude = longitude;
            loc.Latitude = latitide;
            context.Locations.AddObject(loc);
            context.SaveChanges();
            return 0;
   
        }

        public Model.Location GetLocation(int locationId, ref Model.NutriLeafDBEntities context)
        {
            if (context.Locations.Where(p => p.LocationID == locationId).Count() != 0)
            {
                return context.Locations.Where(p => p.LocationID == locationId).First();
            }
            else
                return null;
       
        }

        public void RemoveLocation(int locationId, ref Model.NutriLeafDBEntities context)
        {
            if (context.Locations.Where(p => p.LocationID == locationId).Count() != 0)
            {
                context.DeleteObject(context.Locations.Where(p => p.LocationID == locationId).First());
                context.SaveChanges();

            }
        }

        public void UpdateLocation(Model.Location location, ref Model.NutriLeafDBEntities context)
        {
            throw new NotImplementedException();
        }
    }
}